local hx__xinian = fk.CreateSkill{
  name = "hx__xinian",
  tags = { Skill.Limited },
}

hx__xinian:addEffect(fk.EventPhaseStart, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(hx__xinian.name) and target.phase == Player.Start and player:usedSkillTimes(hx__xinian.name, Player.HistoryGame) == 0
    and player:getMark("@yanjun__xian") >= player.hp * 2
  end,
  can_wake = function(self, event, target, player, data)
    return player:getMark("@yanjun__xian") >= player.hp * 2
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choices = {}
    for i = player.hp * 2, player:getMark("@yanjun__xian") do
      table.insert(choices, tostring(i))
    end
    local choice = room:askToChoice(player,{
      choices = choices,
      skill_name = "hx__xinian",
      cancelable = false,
    })
    if choice then
      local choice_change = room:askToChoices(player,{
        choices = {"#hx__guanyi_change1", "#hx__guanyi_change2", "#hx__yixian_change"},
        min_num = 0,
        max_num = math.max(1, player.hp),
        skill_name = "hx__xinian",
        cancelable = false,
      })
      for _, choice in ipairs(choice_change) do
        if choice == "#hx__yixian_change" then
          room:addPlayerMark(player, "hx__yixian-change", 1)
        elseif choice == "#hx__guanyi_change1" then
          room:addPlayerMark(player, "hx__guanyi-change1", 1)
        elseif choice == "#hx__guanyi_change2" then
          room:addPlayerMark(player, "hx__guanyi-change2", 1)
        end
      end
      room:removePlayerMark(player, "@yanjun__xian", tonumber(choice))
    end
  end,
})

return hx__xinian
